/**
 * @Author:  renyc
 * @Date: 2022年12月15日14:11:41
 */

'use strict';

import * as Cesium from "cesium/Cesium";
import Animation from "./Animation";
import OverlayType from "../OverlayType";
import Transformer from "../../utils/Transformer";
import Util from "../../utils/Util";
const ScanSegmentShader  = require('./shader/radar/RadarScanShader.glsl');
 
class Radar extends Animation{
     constructor(position, radius, options = {}){
        super();

        this._viewer = undefined;
        this._position = position;
        this._radius = radius || 100;
        this._color = options.color || Cesium.Color.GREEN;
        this._speed = options.speed || 3
        this._delegate = undefined;
        this._options = options;

        this._randarArr = [];

        this._type = OverlayType.RADAR;
     }
 
     /**
      * 将Layer挂载到Viewer上(内部方法)
      * @private
      * @Override
      */
    _attach(){
        this._viewer = this._layer._viewer;
        return true;
      }
 
    /**
       * 将Layer从Viewer上卸载(内部方法)
       * @private
       * @Override
       */
    _detach(){
        this.stop();
        this._viewer = undefined;
    }

    
    /**
     *
     * @returns {RadarScan}
     */
    start() {
        !this._delegate && this._createRadar();
        this._delegate && this._viewer.scene.postProcessStages.add(this._delegate);
        return this;
    }

    /**
     *
     * @returns {RadarScan}
     */
    stop() {
        this._delegate &&
            this._viewer.scene.postProcessStages.remove(this._delegate);
        this._delegate = undefined;
        return this;
    }


    /**
     * 创建雷达
     * @param {*} params 
     */
    _createRadar() {
        let lng = this._position.lng;
        let lat = this._position.lat;
        let alt = 0.0;
        let scanColor = this._color;
        let duration = this._speed;
        let radius = this._radius;

        //const { lat, lng, alt = 0, scanColor, duration, radius } = params
        const geographySpace = new Cesium.Cartographic(Cesium.Math.toRadians(lng), Cesium.Math.toRadians(lat), alt);
        // 扫描颜色
        const color = scanColor || new Cesium.Color(119 / 255, 214 / 255, 209 / 255, 0.6)
        // 持续时间 毫秒
        var time = duration || 4000;
        // 绘制平面雷达扫描线
        var radarRadius = radius || 3000
        const radar = this._addRadarScanPostStage(geographySpace, radarRadius, color, time);
        this._randarArr.push(radar);

        return radar;
    }

    _addRadarScanPostStage(cartographicCenter, radius, scanColor, duration){
        var _Cartesian3Center = Cesium.Cartographic.toCartesian(cartographicCenter);
        var _Cartesian4Center = new Cesium.Cartesian4(_Cartesian3Center.x, _Cartesian3Center.y, _Cartesian3Center.z, 1);

        var _CartographicCenter1 = new Cesium.Cartographic(cartographicCenter.longitude, cartographicCenter.latitude, cartographicCenter.height + 500);
        var _Cartesian3Center1 = Cesium.Cartographic.toCartesian(_CartographicCenter1);
        var _Cartesian4Center1 = new Cesium.Cartesian4(_Cartesian3Center1.x, _Cartesian3Center1.y, _Cartesian3Center1.z, 1);

        var _CartographicCenter2 = new Cesium.Cartographic(cartographicCenter.longitude + Cesium.Math.toRadians(0.001), cartographicCenter.latitude, cartographicCenter.height);
        var _Cartesian3Center2 = Cesium.Cartographic.toCartesian(_CartographicCenter2);
        var _Cartesian4Center2 = new Cesium.Cartesian4(_Cartesian3Center2.x, _Cartesian3Center2.y, _Cartesian3Center2.z, 1);
        var _RotateQ = new Cesium.Quaternion();
        var _RotateM = new Cesium.Matrix3();

        var _time = (new Date()).getTime();

        var _scratchCartesian4Center = new Cesium.Cartesian4();
        var _scratchCartesian4Center1 = new Cesium.Cartesian4();
        var _scratchCartesian4Center2 = new Cesium.Cartesian4();
        var _scratchCartesian3Normal = new Cesium.Cartesian3();
        var _scratchCartesian3Normal1 = new Cesium.Cartesian3();

        var ScanPostStage = new Cesium.PostProcessStage({
        fragmentShader: ScanSegmentShader,
        uniforms: {
            u_scanCenterEC: () => {
            return Cesium.Matrix4.multiplyByVector(this._viewer.camera.viewMatrix, _Cartesian4Center, _scratchCartesian4Center);
            },
            u_scanPlaneNormalEC: () => {
            var temp = Cesium.Matrix4.multiplyByVector(this._viewer.camera.viewMatrix, _Cartesian4Center, _scratchCartesian4Center);
            var temp1 = Cesium.Matrix4.multiplyByVector(this._viewer.camera.viewMatrix, _Cartesian4Center1, _scratchCartesian4Center1);
            _scratchCartesian3Normal.x = temp1.x - temp.x;
            _scratchCartesian3Normal.y = temp1.y - temp.y;
            _scratchCartesian3Normal.z = temp1.z - temp.z;

            Cesium.Cartesian3.normalize(_scratchCartesian3Normal, _scratchCartesian3Normal);
            return _scratchCartesian3Normal;
            },
            u_radius: radius,
            u_scanLineNormalEC: () => {
            var temp = Cesium.Matrix4.multiplyByVector(this._viewer.camera.viewMatrix, _Cartesian4Center, _scratchCartesian4Center);
            var temp1 = Cesium.Matrix4.multiplyByVector(this._viewer.camera.viewMatrix, _Cartesian4Center1, _scratchCartesian4Center1);
            var temp2 = Cesium.Matrix4.multiplyByVector(this._viewer.camera.viewMatrix, _Cartesian4Center2, _scratchCartesian4Center2);

            _scratchCartesian3Normal.x = temp1.x - temp.x;
            _scratchCartesian3Normal.y = temp1.y - temp.y;
            _scratchCartesian3Normal.z = temp1.z - temp.z;

            Cesium.Cartesian3.normalize(_scratchCartesian3Normal, _scratchCartesian3Normal);

            _scratchCartesian3Normal1.x = temp2.x - temp.x;
            _scratchCartesian3Normal1.y = temp2.y - temp.y;
            _scratchCartesian3Normal1.z = temp2.z - temp.z;

            var tempTime = (((new Date()).getTime() - _time) % duration) / duration;
            // 第二个参数为 扫描幅度
            Cesium.Quaternion.fromAxisAngle(_scratchCartesian3Normal, tempTime * Cesium.Math.PI * 2, _RotateQ);
            Cesium.Matrix3.fromQuaternion(_RotateQ, _RotateM);
            Cesium.Matrix3.multiplyByVector(_RotateM, _scratchCartesian3Normal1, _scratchCartesian3Normal1);
            Cesium.Cartesian3.normalize(_scratchCartesian3Normal1, _scratchCartesian3Normal1);
            return _scratchCartesian3Normal1;
            },
            u_scanColor: scanColor
        }
        });
        return this._viewer.scene.postProcessStages.add(ScanPostStage);
    }
 }
 
 export default Radar;
 